<?php
class Manager_CategoryProductController extends  Zendvn_Controller_Action{
	private $_arrParam;
	
	private $_mainAction;
	
	protected $_currentController;
	
	public function init()
	{
	    $this->_arrParam = $this->_request->getParams();	
		$this->loadTemplate(TEMPLATE_PATH . '/admin/system');
		$this->_mainAction    = $this->_arrParam['module'] . '/' . $this->_arrParam['controller'] . '/index';	
	    $this->view->arrParam = $this->_arrParam;
	    $tbl_category         = new Manager_Model_categoryproduct();
	    $this->view->list     = $tbl_category->list_category();
	    
	     $tbl_menu = new Manager_Model_menu();
	     $this->view->language=$tbl_menu->get_language();
	   
    }
	
    public function indexAction()
    {
	  
	    $this->view->Title = "Category Product Manager";
		$this->view->headTitle($this->view->Title,true);
		
		$tbl_category = new Manager_Model_categoryproduct();
		
		if($this->_request->getCookie('secnews')==NULL){ $secnews=-1; } 
		else { $secnews=$this->_request->getCookie('secnews');}	 
		
		if($this->_request->getPost('keyword')!=NULL)
		  {
			 $keyword=$this->_request->getParam('keyword');
			 $this->view->keyword=$keyword;
			 if($this->_request->getCookie('status')==NULL){ $status=-1; 
	      } 
	    else 
	      {
	    	  $status=$this->_request->getCookie('status');
	      }

     	  $listArticles = $tbl_category->searchkq($keyword,$status);
          $currentPage = 1;
         	 $i = $this->_getParam('page',1);
         	 if(!empty($i))
         	 {
				 $currentPage = $i;
          	 }
         	 $paginator = Zend_Paginator::factory($listArticles);
         	 $paginator->setItemCountPerPage(10)
                    ->setPageRange(5)
                    ->setCurrentPageNumber($currentPage);
         	 $this->view->paginator = $paginator;
		 } 
    	else 
         {
			if($this->_request->getCookie('status')==NULL){ $status=-1; } 
			else { $status=$this->_request->getCookie('status');}
	 	
     	 	$listArticles = $tbl_category->listItemabout($status,$secnews);
     	 	
     	 	$currentPage = 1;
         	 $i = $this->_getParam('page',1);
         	 if(!empty($i))
         	 {
          	    $currentPage = $i;
          	}
         	 $paginator = Zend_Paginator::factory($listArticles);
         	 $paginator->setItemCountPerPage(45)
                    ->setPageRange(10)
                    ->setCurrentPageNumber($currentPage);
         	 $this->view->paginator = $paginator;
     	 	
     	 	
		}
    
	}

	public function addAction()
	{
	  $this->view->Title = "Category Product Manager [New]";
	  $this->view->headTitle($this->view->Title,true);
	  
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckeditor/ckeditor.js');
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckfinder/ckfinder.js');
	  
	  $tbl_section = new Manager_Model_categoryproduct();
	  
	 if($this->_request->isPost()==true)
		{	
		   $data      =$this->_arrParam;
		   if ($data['title']=='')
		     {
		   	   $error=1;
		       $this->view->error  ='Enter title product';
		      }
		   elseif ($data['section_product_id']=='0')
		      {
		      $error=1;
		      $this->view->error  ='Enter title section product';
		      }
		    elseif ($data['language_id']=='0')
		     {
		      $error=1;
		      $this->view->error  ='Select language';
		     }
		 if (empty($error)==true)
		    {
	          $tbl_section->save($data);	
	          $this->_redirect('manager/category-product');
		    }
		}
	}
   
    public function editAction()
	{
	  $this->view->Title = "Category Product Manager [Edit]";
	  $this->view->headTitle($this->view->Title,true);
	  
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckeditor/ckeditor.js');
	  $this->view->headScript()->appendFile($this->view->baseUrl().'/ckfinder/ckfinder.js');
	  
	  if (!$this->_request->isPost())  
		  {
			$tbl_section = new Manager_Model_categoryproduct();
		  	$id=$this->_request->getParam('id');
			$result=$tbl_section->getrow($id);
			$this->view->data=$result;
		  } 
     	
	}

    public  function deleteAction()
	{
		$tbl_section = new Manager_Model_categoryproduct();
		$id=$this->_request->getParam('id');
		$tbl_section->delete_data($id);
		header("location:".$_SERVER['HTTP_REFERER']);
	}
	
    public function mutiAction()
	{
	    $tblsection=new Manager_Model_categoryproduct();
		$tblsection->delete_muti($this->_arrParam);
		header("location:".$_SERVER['HTTP_REFERER']);
		$this->_helper->viewRenderer->setNoRender();
	}

    public function statusAction()
    {
		  $db = Zend_Registry::get('connectDb');
	      $arrParam=$this->_arrParam;
	      if($this->_arrParam['type'] == 'multi')
	      {
			if(count($arrParam['cid'])>0)
			   {
					$ids = implode(',', $arrParam['cid']);
					$status = ($arrParam['s'] == 0 )? 1:0;  
					$where = 'id IN (' . $ids . ')'; 
				    $db->update('category_product',array('published'=>$status),$where);
			    }
		   }
		else 
		        {
					$status = ($arrParam['s'] == 0 )? 1:0;            
					$where ="id=$arrParam[id]";  
					$db->update('category_product',array('published'=>$status),$where);			
			   	 
		        }
		 header("location:".$_SERVER['HTTP_REFERER']);
		 $this->_helper->viewRenderer->setNoRender();
	}

    public function sortAction()
    { 
		$db = Zend_Registry::get('connectDb');
		if($this->_request->isPost())
		{
			foreach ($this->_arrParam['cid'] as $val)
			 { 
		        $where = 'id = ' . $val;
		        $db->update('category_product',array('order'=>$this->_arrParam['order'][$val]),$where);
		     }
		     header("location:".$_SERVER['HTTP_REFERER']);
		     $this->_helper->viewRenderer->setNoRender();
		} 
    }
}